/**
 * 拉卡拉支付配置
 */
export class LakalaConfig {
  // 基础配置
  static readonly PRODUCTION_URL = 'https://api.lakala.com';
  static readonly SANDBOX_URL = 'https://sandbox-api.lakala.com';
  
  // 商户进件配置
  static readonly MERCHANT_PRODUCTION_URL = 'https://merchant.lakala.com';
  static readonly MERCHANT_SANDBOX_URL = 'https://sandbox-merchant.lakala.com';
  
  // 支付类型
  static readonly PAY_TYPES = {
    WECHAT: 'wechat',
    ALIPAY: 'alipay',
    UNIONPAY: 'unionpay',
    BANKCARD: 'bankcard'
  };
  
  // 商户类型
  static readonly MERCHANT_TYPES = {
    ENTERPRISE: 'enterprise', // 企业商户
    INDIVIDUAL: 'individual'  // 小微商户
  };
  
  // 合同类型
  static readonly CONTRACT_TYPES = {
    SERVICE: 'service',       // 服务合同
    PAYMENT: 'payment',       // 支付合同
    SETTLEMENT: 'settlement'  // 结算合同
  };
  
  // 签署类型
  static readonly SIGN_TYPES = {
    ELECTRONIC: 'electronic', // 电子签署
    PAPER: 'paper'           // 纸质签署
  };
  
  // 商户状态
  static readonly MERCHANT_STATUS = {
    APPLYING: 'applying',     // 申请中
    SUBMITTED: 'submitted',   // 已提交
    APPROVED: 'approved',     // 已通过
    REJECTED: 'rejected',     // 已拒绝
    FAILED: 'failed'         // 失败
  };
  
  // 合同状态
  static readonly CONTRACT_STATUS = {
    APPLYING: 'applying',       // 申请中
    PENDING_SIGN: 'pending_sign', // 待签署
    SIGNED: 'signed',          // 已签署
    REJECTED: 'rejected',      // 已拒绝
    FAILED: 'failed'          // 失败
  };
  
  // Token状态
  static readonly TOKEN_STATUS = {
    ACTIVE: 'active',         // 有效
    EXPIRED: 'expired',       // 已过期
    REVOKED: 'revoked'        // 已撤销
  };
  
  // 文件类型
  static readonly FILE_TYPES = {
    BUSINESS_LICENSE: 'business_license',     // 营业执照
    ID_CARD_FRONT: 'id_card_front',         // 身份证正面
    ID_CARD_BACK: 'id_card_back',           // 身份证背面
    BANK_PERMIT: 'bank_permit',             // 开户许可证
    AUTHORIZATION: 'authorization',          // 授权书
    OTHER: 'other'                          // 其他
  };
  
  // 响应码
  static readonly RESPONSE_CODES = {
    SUCCESS: '000000',
    PARAM_ERROR: '100001',
    SIGN_ERROR: '100002',
    TOKEN_INVALID: '100003',
    MERCHANT_NOT_EXIST: '200001',
    CONTRACT_NOT_EXIST: '300001'
  };
  
  // 超时配置
  static readonly TIMEOUT = {
    DEFAULT: 30000,           // 默认超时30秒
    UPLOAD: 60000,           // 文件上传超时60秒
    DOWNLOAD: 120000         // 文件下载超时120秒
  };
  
  // 重试配置
  static readonly RETRY = {
    MAX_ATTEMPTS: 3,         // 最大重试次数
    DELAY: 1000             // 重试延迟1秒
  };
  
  /**
   * 获取支付配置
   */
  static getPaymentConfig() {
    const isProduction = process.env.NODE_ENV === 'production';
    
    return {
      baseUrl: isProduction ? this.PRODUCTION_URL : this.SANDBOX_URL,
      timeout: this.TIMEOUT.DEFAULT,
      retryAttempts: this.RETRY.MAX_ATTEMPTS,
      retryDelay: this.RETRY.DELAY
    };
  }
  
  /**
   * 获取商户进件配置
   */
  static getMerchantConfig() {
    const isProduction = process.env.NODE_ENV === 'production';
    
    return {
      baseUrl: isProduction ? this.MERCHANT_PRODUCTION_URL : this.MERCHANT_SANDBOX_URL,
      timeout: this.TIMEOUT.DEFAULT,
      uploadTimeout: this.TIMEOUT.UPLOAD,
      downloadTimeout: this.TIMEOUT.DOWNLOAD,
      retryAttempts: this.RETRY.MAX_ATTEMPTS,
      retryDelay: this.RETRY.DELAY
    };
  }
  
  /**
   * 获取文件上传配置
   */
  static getFileConfig() {
    return {
      maxSize: 10 * 1024 * 1024,  // 最大10MB
      allowedTypes: ['jpg', 'jpeg', 'png', 'pdf', 'doc', 'docx'],
      timeout: this.TIMEOUT.UPLOAD
    };
  }
  
  /**
   * 验证环境配置
   */
  static validateConfig() {
    const requiredEnvVars = [
      'LAKALA_APP_ID',
      'LAKALA_APP_SECRET',
      'LAKALA_MERCHANT_NO',
      'LAKALA_PRIVATE_KEY'
    ];
    
    const missing = requiredEnvVars.filter(key => !process.env[key]);
    
    if (missing.length > 0) {
      throw new Error(`Missing required environment variables: ${missing.join(', ')}`);
    }
    
    return true;
  }
}